/*
 * @Description: 项目配置层
 * @Autor: HuiSir<273250950@qq.com>
 * @Date: 2020-11-08 23:25:42
 * @LastEditTime: 2020-11-13 09:30:45
 */
const fs = require('fs')
const ini = require('ini')
const { Log, Print } = require('./src/utils/logger') //日志

//读取配置文件
let MAIN_CONF, Main_PORT, Main_LOGIN_TIMEOUT, DBPort, DBName, DBAccount, DBPass
try {
    //读取
    MAIN_CONF = ini.parse(fs.readFileSync('conf.ini', 'utf-8'))

    //查找配置项
    Main_PORT = MAIN_CONF.Main.port
    Main_LOGIN_TIMEOUT = MAIN_CONF.Main.loginTimeout
    DBPort = MAIN_CONF.Database.port
    DBName = MAIN_CONF.Database.name
    DBAccount = MAIN_CONF.Database.account
    DBPass = MAIN_CONF.Database.pass

    Print.info('配置文件读取成功')
} catch (error) {
    Main_PORT = null
    Main_LOGIN_TIMEOUT = null
    DBPort = null
    DBName = null
    DBAccount = null
    DBPass = null
    Log.warn('配置文件读取失败,将使用默认配置 ' + error.message)
}

module.exports = {
    /* 主域 */
    BASE_URL: 'http://127.0.0.1',

    /* 端口 */
    PORT: Main_PORT || 8080,

    /* 数据库配置 */
    DB: {
        port: DBPort || '27017', //数据库默认为27017端口
        name: DBName || 'myDB', //要连接的数据库名称
        account: DBAccount || null, //此数据库账号(没有可为null)
        pass: DBPass || null, //数据库密码(没有可为null)
    },

    /* 用户登陆过期时间为半小时（以秒计算） */
    LOGIN_TIMEOUT: Main_LOGIN_TIMEOUT || 60 * 30,

    /* api 开放白名单（无需验证token） */
    OPEN_API_LIST: ['/api/user/login', '/api/user/signup'],

    /* 接口请求成功返回数据格式 */
    RES: {
        succ(arg) {
            let resData
            if (typeof arg == 'string') {
                resData = {
                    ok: 1,
                    msg: arg,
                }
            } else {
                const { msg = '成功', data, token } = arg || {}
                resData = {
                    ok: 1,
                    msg: msg,
                    [data && 'data']: data,
                    [token && 'token']: token,
                }
            }
            return resData
        },
        fail(msg = '未知错误') {
            return {
                ok: 0,
                msg,
            }
        },
    },

    /* 错误信息 */
    ERR: {
        e200(msg = '请求失败') {
            let err = new Error(msg)
            err.status = 200
            throw err
        },
        e403(msg = '对不起，您暂未登陆') {
            let err = new Error(msg)
            err.status = 403
            throw err
        },
        e401(msg = '登陆超时') {
            let err = new Error(msg)
            err.status = 401
            throw err
        },
        e404(msg = '路径不存在') {
            let err = new Error(msg)
            err.status = 404
            throw err
        },
        e500(msg = '内部服务器错误') {
            let err = new Error(msg)
            err.status = 500
            throw err
        },
        e502(msg = '数据库错误') {
            let err = new Error(msg)
            err.status = 502
            throw err
        },
        e504(msg = '数据库请求超时') {
            let err = new Error(msg)
            err.status = 504
            throw err
        },
    },
}
